//
//  collectViewController.m
//  Alltelevisionnews
//
//  Created by dllo on 15/12/5.
//  Copyright © 2015年 三人行. All rights reserved.
//

#import "collectViewController.h"

@interface collectViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, strong)UITableView *myTableView;
@property(nonatomic, strong)NSMutableArray *arr;
@end

@implementation collectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor cyanColor];
    self.title = @"我的收藏";
    
    self.myTableView = [[UITableView alloc] init];
    [self.view addSubview:self.myTableView];
    [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.view);
        make.height.equalTo(self.view);
    }];
    self.myTableView.rowHeight = 120;
    
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    self.myTableView.dataSource = self;
    self.myTableView.delegate = self;
}

// 编辑按钮触发方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [self.myTableView setEditing:editing animated:animated];
}
// 实现删除功能
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    baseDataModel *bat = self.arr[indexPath.row];
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // 删除一定要对应数据源的数据
        [self.arr removeObjectAtIndex:indexPath.row];
        [[DataBaseHandle shareDataBasaHandle] deleteNewsInDB:bat];
//        [self.arr removeObjectAtIndex:indexPath.row];
        [self.myTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }
}
// 编辑右边删除名
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"删除";
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.arr.count == 0) {
        UIAlertController *aleryController = [UIAlertController alertControllerWithTitle:@"无收藏" message:[NSString stringWithFormat:@"您还没有收藏新闻"] preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:aleryController animated:YES completion:nil];
        UIAlertAction *leftAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            [self.navigationController popViewControllerAnimated:YES];        }];
        [aleryController addAction:leftAction];
    }
    return self.arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuse = @"reuse";
    collectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[collectTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse];
    }
    
    baseDataModel *baseModel = self.arr[indexPath.row];
    [cell.myImage sd_setImageWithURL:[NSURL URLWithString:baseModel.image]];
    
    return cell;
}

- (void)viewWillAppear:(BOOL)animated {
    self.arr = [NSMutableArray array];
    //    self.arr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
    //
    self.arr = [[DataBaseHandle shareDataBasaHandle] selectArrNews];
    [self.myTableView reloadData];
}
///点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    DetaisViewController *detaisVC = [[DetaisViewController alloc] init];
    detaisVC.strId = [self.arr[indexPath.row] tempId];
    [self.navigationController pushViewController:detaisVC animated:YES];
}









- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
